//
//  FatherViewController.m
//  CleanerAir_Project
//
//  Created by mac on 16/4/28.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "FatherViewController.h"
#import "MBProgressHUD.h"



@interface FatherViewController ()
{
    
    MBProgressHUD	*indicator;
}



@end

@implementation FatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    AppDelegate *delegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
    YRSideViewController *sideViewController=[delegate sideViewController];
    [sideViewController setNeedSwipeShowMenu:NO];
//    self.view.backgroundColor =[UIColor blueColor];
    
    [self Creattitlelabel];
    
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 15, 50, 20)];
    [leftButton setBackgroundImage:[UIImage imageNamed:@"返回.png"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(popViewController) forControlEvents:UIControlEventTouchUpInside];
    //    [leftButton setBackgroundImage:[UIImage imageNamed:@"navbtn.png"] forState:UIControlStateNormal];
    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    
    //键盘回收
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];   
    
}


- (void)keyboardWillShow:(NSNotification *)aNotification
{
    //获取键盘的高度
    /*
     iphone 6:
     中文
     2014-12-31 11:16:23.643 Demo[686:41289] 键盘高度是  258
     2014-12-31 11:16:23.644 Demo[686:41289] 键盘宽度是  375
     英文
     2014-12-31 11:55:21.417 Demo[1102:58972] 键盘高度是  216
     2014-12-31 11:55:21.417 Demo[1102:58972] 键盘宽度是  375
     
     iphone  6 plus：
     英文：
     2014-12-31 11:31:14.669 Demo[928:50593] 键盘高度是  226
     2014-12-31 11:31:14.669 Demo[928:50593] 键盘宽度是  414
     中文：
     2015-01-07 09:22:49.438 Demo[622:14908] 键盘高度是  271
     2015-01-07 09:22:49.439 Demo[622:14908] 键盘宽度是  414
     
     iphone 5 :
     2014-12-31 11:19:36.452 Demo[755:43233] 键盘高度是  216
     2014-12-31 11:19:36.452 Demo[755:43233] 键盘宽度是  320
     
     ipad Air：
     2014-12-31 11:28:32.178 Demo[851:48085] 键盘高度是  264
     2014-12-31 11:28:32.178 Demo[851:48085] 键盘宽度是  768
     
     ipad2 ：
     2014-12-31 11:33:57.258 Demo[1014:53043] 键盘高度是  264
     2014-12-31 11:33:57.258 Demo[1014:53043] 键盘宽度是  768
     */
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
//    int height = keyboardRect.size.height;
//    int width = keyboardRect.size.width;
//    NSLog(@"键盘高度是  %d",height);
//    NSLog(@"键盘宽度是  %d",width);
    self.keyBoardHeight = keyboardRect.size.height;

}

//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification
{
}

#pragma mark - 键盘回收,列表回收
- (void)keyboardHide:(UITapGestureRecognizer *)aTap{
    [self.view endEditing:YES];
    
}

- (void)popViewController{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)Creattitlelabel{

    self.titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 190, 44)];
    self. titlelabel.font = [UIFont boldSystemFontOfSize:18];
    self. titlelabel.textColor = [UIColor whiteColor];
    self. titlelabel.textAlignment = NSTextAlignmentCenter;
    self. titlelabel.text = @"首页";
    self.navigationItem.titleView = self.titlelabel;


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

- (void)createIndicator
{
    if(indicator != nil)
        return;
    
    indicator = [[MBProgressHUD alloc] initWithView:self.view];
    indicator.color = RGB(104, 104, 104);
    indicator.labelFont = [UIFont systemFontOfSize:13];
    indicator.userInteractionEnabled = YES;
    
    [self.view addSubview:indicator];
}
- (void)showIndicator
{
    [self createIndicator];
    
    [self.view bringSubviewToFront:indicator];
    indicator.labelText = @"正在加载...";
    [indicator show:YES];
    
}


- (void)showIndicatorWithDelay:(CGFloat)delay {
    
    [self createIndicator];
    
    [self.view bringSubviewToFront:indicator];
    [indicator show:YES];
    [indicator hide:YES afterDelay:delay];
}

- (void)showIndicator:(NSString*)_labelText
{
    [self createIndicator];
    
    [self.view bringSubviewToFront:indicator];
    indicator.labelText = [NSString stringWithString:_labelText];
    [indicator show:YES];
}

- (void)showTextToast:(NSString *)_labelText
{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.view addSubview:HUD];
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ddddd"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = _labelText;
    [HUD show:YES];
    [HUD hide:YES afterDelay:1.0];
    
}

- (void)showSuccessToast:(NSString *)_labelText withDelay:(NSTimeInterval)delay {
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.view addSubview:HUD];
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OK_Toast"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = _labelText;
    [HUD show:YES];
    [HUD hide:YES afterDelay:delay];
}


- (void)showSuccessToast:(NSString*)_labelText
{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.view addSubview:HUD];
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OK_Toast"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = _labelText;
    [HUD show:YES];
    [HUD hide:YES afterDelay:1];
}

- (void)showErrorToast:(NSString*)_labelText
{
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
    [self.view addSubview:HUD];
    HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error_Toast"]];
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.animationType = MBProgressHUDAnimationFade;
    HUD.labelText = _labelText;
    [HUD show:YES];
    [HUD hide:YES afterDelay:1];
}
- (void)hideIndicator
{
    [indicator hide:YES];
}
#pragma mark - 颜色转换 IOS中十六进制的颜色转换为UIColor
- (UIColor *) colorWithHexString: (NSString *)color
{
    NSString *cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
    
    // String should be 6 or 8 characters
    if ([cString length] < 6) {
        return [UIColor clearColor];
    }
    
    // strip 0X if it appears
    if ([cString hasPrefix:@"0X"])
        cString = [cString substringFromIndex:2];
    if ([cString hasPrefix:@"#"])
        cString = [cString substringFromIndex:1];
    if ([cString length] != 6)
        return [UIColor clearColor];
    
    // Separate into r, g, b substrings
    NSRange range;
    range.location = 0;
    range.length = 2;
    
    //r
    NSString *rString = [cString substringWithRange:range];
    
    //g
    range.location = 2;
    NSString *gString = [cString substringWithRange:range];
    
    //b
    range.location = 4;
    NSString *bString = [cString substringWithRange:range];
    
    // Scan values
    unsigned int r, g, b;
    [[NSScanner scannerWithString:rString] scanHexInt:&r];
    [[NSScanner scannerWithString:gString] scanHexInt:&g];
    [[NSScanner scannerWithString:bString] scanHexInt:&b];
    
    return [UIColor colorWithRed:((float) r / 255.0f) green:((float) g / 255.0f) blue:((float) b / 255.0f) alpha:1.0f];
}


@end
